package com.jswlw.service.base.impl;

import com.jswlw.dao.base.UserMapper;
import com.jswlw.dao.base.UserRoleMapper;
import com.jswlw.entity.base.User;
import com.jswlw.entity.base.UserRole;
import com.jswlw.entity.biz.Distributor;
import com.jswlw.service.base.UserService;
import com.jswlw.service.biz.DistributorService;
import com.jswlw.vo.AdminUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jswlw.common.Constants.*;

/**
 * Created by wu on 2017-12-07.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    DistributorService distributorService;

    public List<User> findAll(Map condition) {
        return userMapper.findAll(condition);
    }

    public User findOne(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public int insert(User user) {
        return userMapper.insertSelective(user);
    }

    public int update(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public int delete(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    public int findCount(Map map) {
        return userMapper.findCount(map);
    }

    public List<User> searchPage(Map map) {
        return null;
    }

    public Map login(String loginName, String password) {
        Map map = new HashMap();
        User user = userMapper.find4LoginName(loginName);
        if (user == null) {
            map.put(CODE, -1);
            map.put(MSG, "用户不存在");
            return map;
        }

        String enPass = password;
        if (!enPass.equals(user.getPassword())) {
            map.put(CODE, -1);
            map.put(MSG, "用户名或者密码错误");
        } else {
            map.put(CODE, 0);
            map.put(MSG, "查询成功");
            //查询经销商信息
            if (!loginName.equals("admin")) {
                Distributor distributor = distributorService.find4Id(Integer.parseInt(user.getLoginName()));
                List<Integer> childrenIds = distributorService.getChildrenIds(distributor.getId());
                distributor.setChildrenIds(childrenIds);
                user.setDistributor(distributor);
            }
            map.put(DATA, user);
        }

        return map;
    }

    @Override
    public User find4Id(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<UserRole> findUserRole(Integer userId) {
        Map condition = new HashMap();
        condition.put("userId", userId);
        return userRoleMapper.findAll(condition);
    }

    @Override
    public void saveUserAuth(Integer[] roles, Integer userId) {
        userRoleMapper.deleteByPrimaryKey(userId);
        if (roles != null) {
            for (Integer roleId : roles) {
                UserRole adminUserRole = new UserRole();
                adminUserRole.setRoleId(roleId);
                adminUserRole.setUserId(userId);
                userRoleMapper.insertSelective(adminUserRole);
            }
        }
    }

    @Override
    public List<AdminUserVo> findUserAndRole() {
        return userMapper.findUserAndRole();
    }

    @Override
    public void delete4LoginName(String loginName) {
        userMapper.delete4LoginName(loginName);
    }

    @Override
    public User find4OpenId(String openId) {
        return userMapper.find4OpenId(openId);
    }

    @Override
    public User find4LoginName(String loginName) {
        return userMapper.find4LoginName(loginName);
    }
}
